package cn.edu.xmu.restfuldemo.dao;

import cn.edu.xmu.restfuldemo.mapper.GoodsMapper;
import cn.edu.xmu.restfuldemo.mapper.ProductsMapper;
import cn.edu.xmu.restfuldemo.model.Goods;
import cn.edu.xmu.restfuldemo.model.GoodsPo;
import cn.edu.xmu.restfuldemo.model.Product;
import cn.edu.xmu.restfuldemo.model.ProductPo;

import cn.edu.xmu.restfuldemo.util.ReturnObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ProductsDao {

    private Logger logger = LoggerFactory.getLogger(ProductsDao.class);
    @Autowired
    private ProductsMapper productsMapper;
//    @Autowired
//    private RedisUtil redisUtil;
    @Value("${restfuldemo.goods.expiretime}")
    private long goodsTimeout;

    /**
     * 用ID找product对象
     * @param id 条件
     * @return  ReturnObject<Product>
     */

    public Goods findGoodById(Integer id){
        logger.info("findGoods: id=" + id);
        Goods goods=null;
//        String key = null;
//
//        key = "g_"+id;
//        Goods goods  = (Goods) redisUtil.get(key);

            //找不到，那就去数据库差（redis关闭，去mybatis查找）
            GoodsPo goodsPo = productsMapper.findGoodById(id); //MAPPER层返回的都是Po对象

//        if (null != goods){
//            logger.info("findGoods: hit redis cache, key = "+key);
//            return goods;
//        } //redis存储的get出来是个BO？   mysql中get出来是个PO？

        if (goodsPo==null) return  null;

            logger.info("findGoods: GoodsPo =" + goodsPo);
            goods = new Goods(goodsPo);//创建BO对象，便于放入cache
//            logger.info("findGoods: put into redis cache, key = "+key);
//            if (goodsPo != null) {
//                redisUtil.set(key, goods, goodsTimeout);
//            }else{
//                redisUtil.set(key, null, goodsTimeout);
//            }


            logger.info("findGoods: retGoods = "+ goods);
            return goods;



    }


    public ReturnObject<Product> findProductById(Integer id){
        logger.info("findProduct: id=" + id);
        Product product=null;
//        String key = null;
//
//        key = "p_"+id;//自己设置key为p_id

//            Product product  = (Product) redisUtil.get(key);
//        if (null != product){
//            logger.info("findProduct: hit redis cache, key = "+key);
//            return new ReturnObject<>(product);
//        } //redis存储的get出来是个BO？   mysql中get出来是个PO？


            ProductPo productPo = productsMapper.findProductById(id); //MAPPER层返回的都是Po对象
            logger.info("findProduct: productPo =" + productPo);
            product = new Product(productPo);//创建BO对象，便于放入cache
//            logger.info("findProduct: put into redis cache, key = "+key);
//            if (productPo != null) {
//                redisUtil.set(key, product, goodsTimeout);
//            }else{
//                redisUtil.set(key, null, goodsTimeout);
//            }


            logger.info("findProduct: retProduct = "+ product);
            return new ReturnObject<>(product);




    }



}
